import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Region } from './regions/entities/region.entity';
import { RegionsModule } from './regions/regions.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { FarmModule } from './farm/farm.module';
import { Farm } from './farm/entities/farm.entity';

@Module({
  imports: [
    RegionsModule,
    ConfigModule.forRoot({ isGlobal: true }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get<string>('DATABASE_HOST'),
        port: +configService.get<string>('DATABASE_PORT'),
        username: configService.get<string>('DATABASE_USERNAME'),
        password: configService.get<string>('DATABASE_PASSWORD'),
        database: configService.get<string>('DATABASE_SCHEMA'),
        entities: [Region, Farm],
        synchronize: false,
        logging: ['error', 'warn', 'query']
      }),
      inject: [ConfigService]
    }),
    FarmModule
  ],
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {}
